package com.tedu.ivos.application.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tedu.ivos.application.pojo.dto.ApplicationQuery;
import com.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import com.tedu.ivos.application.pojo.vo.ApplicationVO;
import com.tedu.ivos.application.service.ApplicationService;
import com.tedu.ivos.base.response.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@Slf4j
@Api(tags = "用车申请模块")
@RequestMapping("/v1/application/")
public class ApplicationController {

    @Autowired
    private ApplicationService applicationService;

    @ApiOperation("1.保存申请单")
    @ApiOperationSupport(order = 10)
    @PostMapping("save")
    public JsonResult saveApplication(ApplicationSaveParam applicationSaveParam) {
        log.debug("保存申请单,{}", applicationSaveParam);
        applicationService.saveApplication(applicationSaveParam);

        return JsonResult.ok();
    }

    @ApiOperation("2.查询申请单")
    @ApiOperationSupport(order = 20)
    @PostMapping("select")
    public JsonResult selectApplicationList(ApplicationQuery applicationQuery) {
        log.debug("查询申请单,{}", applicationQuery);
        List<ApplicationVO> list = applicationService.selectApplicationList(applicationQuery);

        return JsonResult.ok(list);
    }

    @ApiOperation("3.撤销申请单")
    @ApiOperationSupport(order = 30)
    @PostMapping("cancel/{id}")
    public JsonResult cancelApplication(@PathVariable Long id) {
        log.debug("撤销申请单,{}", id);
        applicationService.cancelApplication(id);

        return JsonResult.ok();
    }

    @ApiOperation("4.为申请单分配车辆")
    @ApiOperationSupport(order = 40)
    @PostMapping("distribute/{applicationId}/{vehicleId}")
    public JsonResult distributeApplication(@PathVariable Long applicationId,
                                            @PathVariable Long vehicleId) {
        log.debug("为申请单分配车辆,{}", applicationId);
        applicationService.distribute(applicationId, vehicleId);

        return JsonResult.ok();
    }

    @ApiOperation("5.还车")
    @ApiOperationSupport(order = 50)
    @PostMapping("back/{applicationId}/{vehicleId}")
    public JsonResult backVehicle(@PathVariable Long applicationId,
                                  @PathVariable Long vehicleId) {
        log.debug("还车,{}", applicationId);
        applicationService.back(applicationId, vehicleId);

        return JsonResult.ok();
    }
}
